package com.wteam.dragon.system.apps.generator.controller;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wteam.dragon.system.aop.log.log.Log;
import com.wteam.dragon.system.apps.generator.pojo.bean.ColumnInfo;
import com.wteam.dragon.system.apps.generator.pojo.bean.GenConfig;
import com.wteam.dragon.system.apps.generator.pojo.dto.TableInfoDto;
import com.wteam.dragon.system.apps.generator.pojo.vo.GenConfigVo;
import com.wteam.dragon.system.apps.generator.service.GeneratorService;
import com.wteam.dragon.system.basebean.bean.ResultMessage;
import com.wteam.dragon.system.basebean.vo.PageVo;
import com.wteam.dragon.system.util.BeanUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 代码自动生成
 * @Author 陈晓辉
 * @Date 2021/5/28 15:28
 */
@RestController
@Api(tags = "源：代码自动生成")
@RequestMapping("generator")
@RequiredArgsConstructor
public class GeneratorController {
    private final GeneratorService generatorService;

    @Log("代码生成")
    @ApiOperation("代码生成")
    @GetMapping("generator")
    @PreAuthorize("@el.check('generator')")
    public ResultMessage generator(String tableName){
        return generatorService.generator(tableName);
    }

    @Log("获得数据库表数据")
    @ApiOperation("获得数据库表数据")
    @GetMapping("pageTables")
    @PreAuthorize("@el.check('pageTables')")
    public ResultMessage pageTables(PageVo<TableInfoDto> pageVo, String tableName){
        Page<TableInfoDto> page = BeanUtil.fromPage(pageVo);
        tableName = tableName == null ? "%%" : "%"+ tableName +"%";
        return generatorService.pageTables(page, tableName);
    }

    @Log("获得字段数据")
    @ApiOperation("获得字段数据")
    @GetMapping("listColumns")
    @PreAuthorize("@el.check('listColumns')")
    public ResultMessage listColumns(String tableName){
        return generatorService.listColumns(tableName);
    }

    @Log("同步字段数据")
    @ApiOperation("同步字段数据")
    @PutMapping("syncColumns")
    @PreAuthorize("@el.check('syncColumns')")
    public ResultMessage syncColumns(String tableName){
        return generatorService.syncColumns(tableName);
    }

    @Log("更新字段数据")
    @ApiOperation("更新字段数据")
    @PutMapping("editColumns")
    @PreAuthorize("@el.check('editColumns')")
    public ResultMessage editColumns(String columnInfoListJson){
        JSONArray jsonArray = JSONUtil.parseArray(columnInfoListJson);
        List<ColumnInfo> list = JSONUtil.toList(jsonArray, ColumnInfo.class);
        return generatorService.editColumns(list);
    }


    @Log("更新配置")
    @ApiOperation("更新配置")
    @PutMapping("editGenConfig")
    @PreAuthorize("@el.check('editGenConfig')")
    public ResultMessage editGenConfig(GenConfigVo genConfigVo){
        GenConfig genConfig = BeanUtil.from(genConfigVo, GenConfig.class);
        return generatorService.editGenConfig(genConfig);
    }

    @Log("获得配置")
    @ApiOperation("获得配置")
    @GetMapping("getGenConfig")
    @PreAuthorize("@el.check('getGenConfig')")
    public ResultMessage getGenConfig(String tableName){
        return generatorService.getGenConfig(tableName);
    }

}
